package com.tdcy.biz.scmgt.controller;

import java.io.File;
import java.io.IOException;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.tdcy.biz.scmgt.service.bean.OtherMatBean;
import com.tdcy.biz.wxmgt.service.WeixinService;
import com.tdcy.framework.BaseController;
import com.tdcy.framework.bean.PageInfo;
import com.tdcy.framework.util.WebUtils;
import com.tdcy.sys.service.IFileSvc;

/**
*/
@Controller
@RequestMapping(value = "/othermat")
public class WxOtherMatController extends BaseController {
	@Autowired
	WeixinService wxMpService;

	@Autowired
	IFileSvc fileSvc;

	@ResponseBody
	@RequestMapping(value = "/materialNewsBatchGet", method = RequestMethod.POST)
	public Object materialNewsBatchGet(OtherMatBean otherMatBean)
			throws IOException, WxErrorException {
		WxMpMaterialFileBatchGetResult result = wxMpService
				.getMaterialService().materialFileBatchGet(
						otherMatBean.getType(), otherMatBean.getStartRow(),
						otherMatBean.getRows());
		int total = result.getTotalCount();

		int totalPage = 1;
		if (total % 1000 == 0) {
			totalPage = (int) total / 1000;
		} else {
			totalPage = (int) total / 1000 + 1;
		}

		PageInfo<WxMaterialFileBatchGetNewsItem> pageData = new PageInfo<WxMaterialFileBatchGetNewsItem>();
		pageData.setTotalRecord((int) result.getTotalCount());
		pageData.setTotalPage(totalPage);
		pageData.setResultsList(result.getItems());
		pageData.setPageSize(otherMatBean.getRows());
		pageData.setCurPageNO(otherMatBean.getPage());

		return WebUtils.createJSONSuccess("成功");
	}

	@ResponseBody
	@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
	public Object uploadImage(
			@RequestParam(value = "matImage", required = false) MultipartFile file)
			throws IOException, WxErrorException {

		String rootPath = WxOtherMatController.class.getClass()
				.getResource("/").getPath();
		String tempPath = rootPath + File.separator + "temp";
		File tempFolder = new File(tempPath);
		if (!tempFolder.exists()) {
			tempFolder.mkdirs();
		}
		File outFile = new File(tempPath + File.separator
				+ System.currentTimeMillis());
		if (!outFile.exists()) {
			outFile.createNewFile();
		}
		FileUtils.copyInputStreamToFile(file.getInputStream(), outFile);
		WxMediaImgUploadResult result = wxMpService.getMaterialService()
				.mediaImgUpload(outFile);
		outFile.delete();

		return WebUtils.createJSONSuccess("成功", result.getUrl());
	}

	@ResponseBody
	@RequestMapping(value = "/addOthermat", method = RequestMethod.POST)
	public Object addOthermat(WxMpMaterial wxMpMaterial,
			@RequestParam(value = "type", required = true) String type,
			@RequestParam(value = "fileId", required = false) String fileId)
			throws IOException, WxErrorException {

		File outFile = fileSvc.getFileFile(Integer.parseInt(fileId));

		wxMpMaterial.setFile(outFile);
		WxMpMaterialUploadResult result = wxMpService.getMaterialService()
				.materialFileUpload(type, wxMpMaterial);

		return WebUtils.createJSONSuccess("成功", result);
	}
}
